package day3;

import java.util.Random;

//a.有一个抽奖池,该抽奖池中存放了奖励的金额,
//该抽奖池用一个数组int[] arr = {10,5,20,50,100,200,500,800,2,80,300};
//创建两个抽奖箱(线程)设置线程名称分别为“抽奖箱1”，“抽奖箱2”，随机从arr数组中获取奖项元素并打印在控制台上,格式如下:
//抽奖箱1 又产生了一个 10 元大奖
//抽奖箱2 又产生了一个 100 元大奖
////.....
public class Work3 implements Runnable{
    int[] arr = {10,5,20,50,100,200,500,800,2,80,300};
    boolean[] flag=new boolean[arr.length];
    int count;
    public void run() {
        String name=Thread.currentThread().getName();
        Random r=new Random();
        while(true){
            synchronized (this){
                int i=r.nextInt(arr.length);
                int result=arr[i];
                if(!flag[i]){
                    flag[i]=true;
                    System.out.println(name+"产生了一个奖项:"+result+"元大奖");
                    count++;
                }
                if(count==arr.length) break;
            }
        }
    }
    public static void main(String[] args) {
        Work3 w=new Work3();
        new Thread(w,"抽奖箱1").start();
        new Thread(w,"抽奖箱2").start();
    }
}
